const { getPool } = require('../../../config/db')

const HanziCharacter = {
  async getAllHanziCharacters() {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('SELECT * FROM hanzi_character')
    return rows
  },

  async getHanziCharacterById(id) {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('SELECT * FROM hanzi_character WHERE id = ?', [id])
    return rows[0]
  },

  async createHanziCharacter(hanziData) {
    const mysqlPool = await getPool()
    console.log('hanziData:', hanziData)
    const [result] = await mysqlPool.execute('INSERT INTO hanzi_character (`character`, structure_id, radical_id, svg_path, stroke_count) VALUES (?, ?, ?, ?, ?)', [hanziData.character, hanziData.structure_id, hanziData.radical_id, hanziData.svg_path, hanziData.stroke_count])
    return result.insertId
  },

  async updateHanziCharacter(id, hanziData) {
    const mysqlPool = await getPool()
    await mysqlPool.execute('UPDATE hanzi_character SET `character`=?, structure_id=?, radical_id=?, svg_path=?, stroke_count=? WHERE id=?', [hanziData.character, hanziData.structure_id, hanziData.radical_id, hanziData.svg_path, hanziData.stroke_count, id])
  },

  async deleteHanziCharacter(id) {
    const mysqlPool = await getPool()
    await mysqlPool.execute('DELETE FROM hanzi_character WHERE id = ?', [id])
  },
}

module.exports = HanziCharacter
